<?php
namespace App\Http\Controllers\Mp;

use App\Http\Resources\Mp\BookOrderResource;
use App\Http\Resources\Mp\OfficialOrderResource;
use App\Models\BookOrder;
use App\Models\BookOrderReview;
use Illuminate\Http\Request;
use App\Models\BookRule;
use App\Services\BookOrderService;
/**
 * Class OfficialOrderController
 * @package App\Http\Controllers\Mp
 * @author zjw
 */
class OfficialOrderController extends BaseController{

    public function index(){
        $query = BookOrder::belongToUser()->isOfficial()->with(['rule','review'])->orderBy("created_at",'desc');
        return OfficialOrderResource::collection($query->paginate(10));
    }

    public function store(Request $request){
        $ruleId = $request->rule_id;
        $rule = BookRule::schoolIs(\Auth::user()->school_id)->find($ruleId);
        if(!$rule) {
            return $this->fail('表单填写不完整');
        }
        $order = BookOrderService::generateOfficialOrder(\Auth::user(),$rule );
        BookOrderReview::create([
            'person_info' => $request->person,
            'person_num'=> $request->num,
            'remark'=> $request->remark,
            'reason'=> $request->reason,
            'order_id'=> $order->id
        ]);
        return $order;
    }
}
